package com.linciping.gradle.methodcodeinject

import org.objectweb.asm.Label
import org.objectweb.asm.MethodVisitor
import org.objectweb.asm.commons.AdviceAdapter

class LineNumberMethodVisitor(
    methodVisitor: MethodVisitor?,
    private val changeMethod: ChangeMethod,
    access: Int, name: String?, descriptor: String?
) : AdviceAdapter(
    ASM7, methodVisitor,
    access, name, descriptor,
) {

    private val _lineStartLabelMap = linkedMapOf<Label, Int>()
    val lineStartLabelMap: Map<Label, Int> = _lineStartLabelMap

    override fun visitLineNumber(line: Int, start: Label?) {
        super.visitLineNumber(line, start)
        if (changeMethod.lines.contains(line)) {
            start?.let {
                _lineStartLabelMap[it] = line
            }
        }
    }
}